﻿using MediatR;
using W.Common.Enum;
using W.Warehouse.Domain.AllocationProduct;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.AllocationCommand;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.AllocationCommandHandler
{
    public class AllocationShowCommandHandler:IRequestHandler<AllocationShowCommand,PageDTO<AllocationProductDTO>>
    {
        private readonly IAllocationRepository _iallocationRepository;
        private readonly IAllocationProductRepository _iallocationProductRepository;
        private readonly IWarehouseInfoRepository _iwarehouseInfoRepository;
        public AllocationShowCommandHandler(IAllocationRepository iallocationRepository, IAllocationProductRepository iallocationProductRepository, IWarehouseInfoRepository iwarehouseInfoRepository)
        {
            _iallocationRepository = iallocationRepository;
            _iallocationProductRepository = iallocationProductRepository;
            _iwarehouseInfoRepository = iwarehouseInfoRepository;
        }

        public async Task<PageDTO<AllocationProductDTO>> Handle(AllocationShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<AllocationProductDTO> page = new PageDTO<AllocationProductDTO>();
            var wlist = await _iwarehouseInfoRepository.GetAllAsync();
            var alist=await _iallocationRepository.GetAllAsync();
            var aplist = await _iallocationProductRepository.GetAllAsync();
            var list = (from a in wlist
                        join b in alist
                        on a.WarehouseId equals b.WarehouseId
                        join c in aplist
                        on b.AllocationId equals c.AllocationId
                        where c.IsDelete == false && b.IsDelete == false
                        select new AllocationProductDTO
                        {
                            AllocationId=b.AllocationId,
                            AllocationNo = b.AllocationNo,
                            ProductName=c.ProductName,
                            ProductNo=c.ProductNo,
                            ProductBatch=c.ProductBatch,
                            ProductSpec=c.ProductSpec,
                            Quantity = c.Quantity,
                            WarehouseName=a.WarehouseName,
                            BinLocationName=c.BinLocationName,
                            AWarehouseName=b.AWarehouseName,
                            AllocationStatus=b.AllocationStatus,
                            AllocationStatusText = b.AllocationStatus.ToString(),
                            ExaminePeople = b.ExaminePeople,
                            ExamineTime = b.ExamineTime,
                            AllocationProductId=c.AllocationProductId,
                        }).ToList();
            if (!string.IsNullOrEmpty(request.AllocationNo))
            {
                list=list.Where(c=>c.AllocationNo==request.AllocationNo).ToList();
            }
            if (request.CreateTime!=null)
            {
                list = list.Where(c => c.CreateTime == request.CreateTime).ToList();
            }
            if (request.AllocationStatus >0)
            {
                list = list.Where(c => c.AllocationStatus == (AuditStatus)request.AllocationStatus).ToList();
            }
            page.TotalCount = list.Count();
            page.TotalPage = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = list.OrderByDescending(c=>c.AllocationId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            return page;    
        }
    }
}
